<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/17
 * Time: 12:09
 */

namespace app\index\controller;


use app\common\model\AdvPosition;
use app\common\utils\Utils;
use think\facade\View;

class Index extends Base
{
    public function index(){

        $top_hd = AdvPosition::getAdvByPosition(2);
        return View::fetch('index',compact('top_hd'));
    }

    public function qr_code(){
        error_reporting(E_ERROR);
        $url = isset($_GET['data']) ? $_GET['data'] : '5345';
        $url = urldecode($url);
        $head_pic = input('get.head_pic', '');
        $back_img = input('get.back_img', '');
        $valid_date = input('get.valid_date', 0);
        $out_trade_no = time().rand(1, 10000);

        $qr_code_path = 'uploads/qrcode/pay';

        $qr_code_file = Utils::getQRCode($url, $qr_code_path, $out_trade_no);

        /*
        //二维码叠加水印
        $QR = Image::open($qr_code_file);
        $QR_width = $QR->width();
        $QR_height = $QR->height();

        // 添加背景图
        if ($back_img && file_exists($back_img)) {
            $back =Image::open($back_img);
            $back->thumb($QR_width, $QR_height, \think\Image::THUMB_CENTER)
                ->water($qr_code_file, \think\Image::WATER_NORTHWEST, 60);//->save($qr_code_file);
            $QR = $back;
        }

        if ($valid_date && strpos($url, 'weixin.qq.com') !== false) {
            $QR = $QR->text('有效时间 '.$valid_date, "./vendor/topthink/think-captcha/assets/zhttfs/1.ttf", 7, '#00000000', Image::WATER_SOUTH);
        }
        $QR->save($qr_code_file, null, 100);
        */

        $qrHandle = imagecreatefromstring(file_get_contents($qr_code_file));
        unlink($qr_code_file); //删除二维码文件
        header("Content-type: image/png");
        imagepng($qrHandle);
        imagedestroy($qrHandle);
        exit;
    }
}